package cn.edu.neusoft.yyd208.foodorder.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.Button;
import cn.edu.neusoft.yyd208.foodorder.listener.TListener;
import android.widget.EditText;


import cn.edu.neusoft.yyd208.foodorder.R;
import cn.edu.neusoft.yyd208.foodorder.bean.RegisterBean;

import cn.edu.neusoft.yyd208.foodorder.model.RegisterModel;

public class RegisterActivity extends BaseActivity {
    EditText et_userpass,et_username,et_apass,et_number,et_address,et_role;
    Button btn_register;
    String username,userpass,userpass2,tel,address,role;
    TListener<RegisterBean> registerListener=new TListener<RegisterBean>() {
        @Override
        public void onResponse(RegisterBean registerBean) {
            if(registerBean.getSuccess().equals("1")){
                showToast("注册成功");
                returnMsg();
            }
            else {
                showToast("此人已经注销");
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    void initView() {
        setLayout_file(R.layout.activity_register);
        //findViewById()
        et_username=(EditText)findViewById(R.id.et_username);
        et_userpass=(EditText)findViewById(R.id.et_userpass);
        et_apass=(EditText)findViewById(R.id.et_apass);
        et_number=(EditText)findViewById(R.id.et_number);
        et_address=(EditText)findViewById(R.id.et_address);
        et_role=(EditText)findViewById(R.id.et_role);
        btn_register=(Button)findViewById(R.id.btn_register);


    }

    @Override
    void initEvent() {
       //setOnclick
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(et_username.getText().toString().trim().equals("")||et_userpass.getText().toString().trim().equals("")||et_apass.getText().toString().trim().equals("")||et_number.getText().toString().trim().equals("")||et_address.getText().toString().trim().equals("")||et_role.getText().toString().trim().equals("")){
                    showToast("不能为空");
                }
                else {
                    register();
                    saveUser();
                    if (userpass.equals(userpass2)){
                        RegisterModel registerModel=new RegisterModel();
                        registerModel.getRegisterMessage(username,userpass,tel,address,role,registerListener);
                    }
                    else {
                        showToast("两次输入密码不一致");
                    }
//                    Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    //把用户名和密码放到服务器上验证
                }
            }
        });
    }
    void register(){
    username=et_username.getText().toString();
    userpass=et_userpass.getText().toString();
    userpass2=et_apass.getText().toString();
    tel=et_number.getText().toString();
    address=et_address.getText().toString();
    role=et_role.getText().toString();
    }
    @Override
    void initData() {

    }
    void returnMsg(){
        Intent intent=new Intent();
        intent.putExtra("user",username);
        setResult(RESULT_OK,intent);
        finish();
    }
    void saveUser(){
        SharedPreferences sp=getSharedPreferences("userInfo",MODE_PRIVATE);
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",et_username.getText().toString());
        editor.putString("userpass",et_userpass.getText().toString());
        editor.putString("userpass2",et_apass.getText().toString());
        editor.putString("tel",et_number.getText().toString());
        editor.putString("address",et_address.getText().toString());
        editor.putString("role",et_role.getText().toString());
        editor.commit();
    }
}
